/* -----------------
* brief
* 1. this is code gen by tools
* 2. for add/update/delete/query the data save in sql database
 */

package appmanager

import (
	"errors"
	"github.com/go-xorm/xorm"
	"github.com/mjiulee/lego"
	//"github.com/mjiulee/lego/utils"
)

/* 根据id获取记录
 * -----------------
 */
func (t *AppSuit ) RecodeById(id int64) *AppSuit  {
	item := new(AppSuit )
	_, err := lego.GetDBEngine().ID(id).Where("if_del = ?", 0).Get(item)
	if item.Id <= 0 || err != nil {
		return nil
	}
	return item
}

func (t *AppSuit ) RecodeBySuitCode(appSuitCode int64) *AppSuit  {
	item := new(AppSuit )
	item.AppSuitCode = appSuitCode
	_, err := lego.GetDBEngine().Where("if_del = ?", 0).Get(item)
	if item.Id <= 0 || err != nil {
		return nil
	}
	return item
}
/* 添加
 * -----------------
 */
func (t *AppSuit ) AddRecode(item2add *AppSuit ) bool {
	item2add.Id = lego.UUID()
	_, err := lego.GetDBEngine().Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *AppSuit ) AddRecodeWithsession(session*xorm.Session,item2add *AppSuit ) bool {
	item2add.Id = lego.UUID()
	_, err := session.Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 删除(硬删除)*/
func (t *AppSuit ) DelRecodeById(id int64) bool {
	item := new(AppSuit )
	_, err := lego.GetDBEngine().ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *AppSuit ) DelRecodeByIdWithSession(session*xorm.Session,id int64) bool {
	item := new(AppSuit )
	_, err := session.ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 删除(软删除)*/
func (t *AppSuit ) SDelRecodeById(id int64) bool {
	item := new(AppSuit )
	item.IfDel = 1
	_, err := lego.GetDBEngine().ID(id).Cols("if_del").Update(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *AppSuit ) SDelRecodeByIdWithSession(session*xorm.Session,id int64) bool {
	item := new(AppSuit )
	item.IfDel = 1
	_, err := session.ID(id).Cols("if_del").Update(item)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}



/* 更新
 * -----------------*/
func (t *AppSuit ) UpdateRecode(rc *AppSuit ) bool {
	_, err := lego.GetDBEngine().ID(rc.Id).Omit("id", "if_del").Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *AppSuit) UpdateRecodeFields(rc *AppSuit, cols ...string) bool {
	_, err := lego.GetDBEngine().Id(rc.Id).Cols(cols...).Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *AppSuit ) UpdateRecodeWithSession(session *xorm.Session,rc *AppSuit ) bool {
	_, err := session.ID(rc.Id).Omit("id", "if_del").Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}


func (t *AppSuit) List(keyword string, page, psize int) ([]AppSuit, int64, error) {
	if page < 1 {
		return nil, 0, errors.New("page must greate than 1")
	}
	start := (page-1*psize)
	resultList := make([]AppSuit, 0)

	session := lego.GetDBEngine().Table(AppSuit{}).Alias("t1").
		//Join("INNER", []string{"group", "t2"}, "group.id = t2.group_id").
		//Join("INNER", "tb_xx", "tb_xx.id = tb_xx.type_id").
		Where("if_del = ?", 0)
		//And("fxxx = ?", 0).
		//Desc("t1.field_xxx").
		//Asc()
	//OrderBy("asc")

	session2 := *session.Clone()
	err := session.Limit(psize, start).Find(&resultList)
	if err != nil {
		return nil, 0, err
	}

	total, err := session2.Count()
	if err != nil {
		return nil, 0, err
	}
	return resultList, total, err
}


/*查询表名, 注意，函数名不能定义未TableName()，因为会与xorm内部使用的TableName冲突*/
func (t *AppSuit ) TName() string {
	tname := lego.GetDBEngine().TableName(t)
	return tname
}